package com.lezhi.socialgram.handle;

import com.lezhi.socialgram.enums.ResultEnum;
import com.lezhi.socialgram.exception.SocialGramException;
import com.lezhi.socialgram.util.Result;
import com.lezhi.socialgram.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by daizhen on 2017/3/26.
 */
@ControllerAdvice
public class ExceptionHandle {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        if (e instanceof SocialGramException) {
            SocialGramException userException = (SocialGramException) e;
            return ResultUtils.error(userException);
        } else {
            return ResultUtils.error(ResultEnum.INTERNAL_ERROR);
        }
    }

}
